<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>首页-添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${ctxPath}/static/manage/css/x-admin.css" media="all">
</head>

<body>
<div class="x-body">

    <form id="formAdd" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>电脑品牌</label>
            <div class="layui-input-inline typeSelect">
                <select name="categoryId" id="categoryId" lay-filter="typeArticle">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red"></span>系列</label>
            <div class="layui-input-inline">
                <input type="text" id="item" name="item" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red"></span>标题</label>
            <div class="layui-input-inline">
                <input type="text" id="title" name="title" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red"></span>卖点</label>
            <div class="layui-input-inline">
                <input type="text" id="point" name="point" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red"></span>单价</label>
            <div class="layui-input-inline">
                <input type="text" id="price" name="price" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red"></span>库存</label>
            <div class="layui-input-inline">
                <input type="text" id="num" name="num" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label  class="layui-form-label">图片</label>

            <div class="layui-col-md3 layui-col-xs5">
                <div class="layui-upload-list thumbBox mag0 magt3">
                    <img class="layui-upload-img thumbImg">
                </div>
            </div>
            <input type="hidden" id="pictureId" name="pictureId" >
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <button class="layui-btn" type="button" lay-filter="add" lay-submit="">增加</button>
        </div>
    </form>

</div>
</body>
</html>
<script src="${ctxPath}/static/manage/layui/layui.js" charset="utf-8"></script>
<script src="${ctxPath}/static/manage/js/x-layui.js" charset="utf-8"></script>
<script src="${ctxPath}/static/manage/js/jquery.js"></script>

<script type="text/javascript">
    layui.use(['form','layer','upload'], function(){
        var $ = layui.$, form = layui.form, upload = layui.upload, layer = layui.layer;

        // 上传缩略图
        upload.render({
            elem: '.thumbBox',
            size: 600,
            url: '/kaptcha/uploadImg',

            before: function(obj){
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                layer.load();
                obj.preview(function(index, file, result){
                    $('.thumbImg').attr('src', result);
                });
            },
            done: function(res){
                // alert("图片上传成功吗，保存后生效");
                layer.closeAll('loading');
                $("#pictureId").val(res.data);
                $('.thumbBox').css("background","#fff");
                if(res.code != 0){
                    alert(res.data);
                }
            }
        });

        //回显类型信息
        $(function () {
            $.ajax({
                type: 'post',
                url: '/manage/computer/computerType',
                success:function(res){
                    if(res.length>0){
                        for(var i=0;i<=res.length;i++){
                            $("#categoryId").append("<option value='"+res[i].id+"'>"+res[i].type+"</option>")
                            form.render();
                        }
                    }
                }
            });
        })


        // 监听提交
        form.on('submit(add)', function(){
            var pictureId = $("#pictureId").val();
            var categoryId=$("#categoryId").val();
            var price=$("#price").val();
            var num=$("#num").val();

            if(categoryId=="" || categoryId.length<0){
                alert("电脑品牌没有选择");
                return false;
            }
            if(pictureId.length <= 0){
                alert("没有选择图片,或图片上传失败");
                return false;
            }
            if(isNaN(price)){
                alert("请输入正确的格式")
                return false;
            }
            if(isNaN(num)){
                alert("请输入正确的格式")
                return false;
            }

            $.ajax({
                url:'/manage/computer/computerAdd',
                type:'post',
                data:$("#formAdd").serialize(),
                success:function(result){
                    alert(result);
                    window.parent.location.reload();
                },
                error:function(result){
                    alert("失败？请及时告知开发人员"+result);
                    window.parent.location.reload();
                }
            });
        });
    });
</script>
